//
//  SASolidStageObject.m
//  GameFrame
//
//  Created by Hiroshi Nomura on 2013/10/28.
//  Copyright (c) 2013年 Hiroshi Nomura. All rights reserved.
//

#import "SASolidStageObject.h"

#import "SAStageObjectRenderer.h"

@interface SASolidStageObject()
{
    GLuint _vertexArrayName;
    GLuint _textureName;
}
@end

@implementation SASolidStageObject

@synthesize renderedVertex;
@synthesize textureImage;
@synthesize skinVertex;
@synthesize skeleton;

-(void)createRenderedVertexAttrib
{
    //VAO
	glGenVertexArraysOES(1, &_vertexArrayName);
	glBindVertexArrayOES(_vertexArrayName);
	
	GLuint vertexBufferName;
	glGenBuffers(1, &vertexBufferName);
	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferName);
	glBufferData(GL_ARRAY_BUFFER, self.renderedVertex->vertexSize, self.renderedVertex->vertex, GL_STATIC_DRAW);
	glEnableVertexAttribArray(VERTEX_ATTRIB_IDX_VERTEX);
	glVertexAttribPointer(VERTEX_ATTRIB_IDX_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
	
	if (self.renderedVertex->normal != NULL) {
		GLuint normalBufferName;
		glGenBuffers(1, &normalBufferName);
		glBindBuffer(GL_ARRAY_BUFFER, normalBufferName);
		glBufferData(GL_ARRAY_BUFFER, self.renderedVertex->normalSize, self.renderedVertex->normal, GL_STATIC_DRAW);
		glEnableVertexAttribArray(VERTEX_ATTRIB_IDX_NORMAL);
		glVertexAttribPointer(VERTEX_ATTRIB_IDX_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Normal), 0);
	}
	
	if (self.renderedVertex->textureCoord != NULL) {
        //Generate Texture Buffer
        glGenTextures(1, &_textureName);
        glBindTexture(GL_TEXTURE_2D, _textureName);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexImage2D(GL_TEXTURE_2D, 0, self.textureImage->format, self.textureImage->width, self.textureImage->height, 0,
                     self.textureImage->format, GL_UNSIGNED_BYTE, self.textureImage->data);
        glGenerateMipmap(GL_TEXTURE_2D);

        //Bind texture buffer to texture attribute
		GLuint textureBufferName;
		glGenBuffers(1, &textureBufferName);
		glBindBuffer(GL_ARRAY_BUFFER, textureBufferName);
		glBufferData(GL_ARRAY_BUFFER, self.renderedVertex->textureCoordSize, self.renderedVertex->textureCoord, GL_STATIC_DRAW);
		glEnableVertexAttribArray(VERTEX_ATTRIB_IDX_TEXTURECOORD0);
		glVertexAttribPointer(VERTEX_ATTRIB_IDX_TEXTURECOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(Texture), 0);
	}
	
    if (self.skinVertex != NULL) {
        GLuint weightmapBufferName;
        glGenBuffers(1, &weightmapBufferName);
        glBindBuffer(GL_ARRAY_BUFFER, weightmapBufferName);
        glBufferData(GL_ARRAY_BUFFER, self.skinVertex->vertexWeightSize, self.skinVertex->vertexWeight, GL_STATIC_DRAW);
        glEnableVertexAttribArray(VERTEX_ATTRIB_IDX_WEIGHTMAP);
        glVertexAttribPointer(VERTEX_ATTRIB_IDX_WEIGHTMAP, 4, GL_FLOAT, GL_FALSE, sizeof(VertexWeight), 0);
	}
    
    if (self.skeleton != NULL) {
        GLuint jointIndexBufferName;
        glGenBuffers(1, &jointIndexBufferName);
        glBindBuffer(GL_ARRAY_BUFFER, jointIndexBufferName);
        glBufferData(GL_ARRAY_BUFFER, self.skinVertex->jointIndexSize, self.skinVertex->jointIndex, GL_STATIC_DRAW);
        glEnableVertexAttribArray(VERTEX_ATTRIB_IDX_JOINTIDX);
        glVertexAttribPointer(VERTEX_ATTRIB_IDX_JOINTIDX, 4, GL_SHORT, GL_FALSE, sizeof(JointIndex), 0);
    }
    
	GLuint elementBufferName;
	glGenBuffers(1, &elementBufferName);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferName);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.renderedVertex->elementSize, self.renderedVertex->element, GL_STATIC_DRAW);
    
    glBindVertexArrayOES(0);
}

-(void)drawObject
{
    glBindTexture(GL_TEXTURE_2D, _textureName);
	glBindVertexArrayOES(_vertexArrayName);
	glCullFace(GL_BACK);
	glDrawElements(GL_TRIANGLES, self.renderedVertex->elementCount, GL_UNSIGNED_SHORT, 0);
}

-(void)updateObject:(GLint*)vtxUniforms
{
    
}

@end